package com.onesports.intelligent.k12.polarlight.util;

import com.github.binarywang.wxpay.bean.ecommerce.SignatureHeader;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Slf4j
public class WxPayUtil {

    /**
     * 获取请求头签名
     * @return
     */
    public static SignatureHeader fetchRequestEcommerceSignatureHeader(HttpServletRequest request) {
        log.info("服务商模式回调请求头:{}",request.getHeaderNames().toString());
        SignatureHeader signatureHeader = new SignatureHeader();
        signatureHeader.setSigned(request.getHeader("Wechatpay-Signature"));
        signatureHeader.setNonce(request.getHeader("Wechatpay-Nonce"));
        signatureHeader.setSerialNo(request.getHeader("Wechatpay-Serial"));
        signatureHeader.setTimeStamp(request.getHeader("Wechatpay-TimeStamp"));
        return signatureHeader;
    }

    /**
     * 获取请求头签名
     * @return
     */
    public static com.github.binarywang.wxpay.bean.notify.SignatureHeader fetchRequest2SignatureHeader(HttpServletRequest request) {
        log.info("普通回调请求头:{}",request.getHeaderNames().toString());
        com.github.binarywang.wxpay.bean.notify.SignatureHeader signatureHeader = new com.github.binarywang.wxpay.bean.notify.SignatureHeader();
        signatureHeader.setSignature(request.getHeader("Wechatpay-Signature"));
        signatureHeader.setNonce(request.getHeader("Wechatpay-Nonce"));
        signatureHeader.setSerial(request.getHeader("Wechatpay-Serial"));
        signatureHeader.setTimeStamp(request.getHeader("Wechatpay-TimeStamp"));
        return signatureHeader;
    }

    /**
     * 获取Rfc3339标准时间格式
     * @param time
     * @return
     */
    public static String getRfc3339(long time){
        String result="";
        try{
            DateTime date = new DateTime(time);
            result = date.toString();
        }catch (Exception e){

        }
        return result;
    }

    public static Date parseRfc3339(String dateStr){
        if(StringUtils.isEmpty(dateStr)){
            return null;
        }
        DateTime date = new DateTime(dateStr);
        return date.toDate();
    }
}
